<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of ChatService
 *
 * @author admin
 */
class Service_ChatService {
    public function __construct()
    {

    }
    public function getChat($id)
    {
        $chatMapper = new Application_Model_ChatMapper();
        $chat = $chatMapper->fetchById($id);

        $chatVO = $this->_toChatVO($chat);

        return $chatVO;
    }

    /**
     * @return array
     */
    public function getChats()
    {   
        $chatMapper = new Application_Model_ChatMapper();
        $chats = $chatMapper->fetchAll();
        
        $chatVOs = $this->_toChatVOs($chats);

        return $chatVOs;
        
    }
    
    /**
     * @return array
     */
    public function getChatsFromProjectId($projectId)
    {
        $chatMapper = new Application_Model_ChatMapper();
        $chats = $chatMapper->fetchByProjectId($projectId);

        //$chatVOs = $this->_toChatVOs($chats);

        //return $chatVOs;
        return $chats;
    }

    /**
     * @return array
     */
    public function getChatsList()
    {
        return $this->getChats();
    }
    
    public function addChat($projectId, $userId, $content)
    {
        $chatMapper = new Application_Model_ChatMapper();
        if($chatMapper->addChat($projectId, $userId, $content))
        {
            return true;
        }
        else{
            return false;
        }
    }

    /**
     * @param Application_Model_Chat $chat
     * @return VO_Chat
     */
    private function _toChatVO($chat)
    {
        
        $chatVO = new VO_Chat();
        $chatVO->id                 = $chat->Id;
        $chatVO->projectId          = $chat->ProjectId;
        $chatVO->userId             = $chat->UserId;
        $chatVO->creationdate       = $chat->Creationdate;
        $chatVO->content            = $chat->Content;

        return $chatVO;
    }

    /**
     * @param array $chats
     * @return array
     */
    private function _toChatVOs($chats = array())
    {
        $chatVOs = array();
        foreach ($chats as $chat) {
            $chatVOs[] = $this->_toChatVO($chat);
        }

        return $chatVOs;
    }
    
    public function logOut()
    {
        Zend_Auth::getInstance()->clearIdentity();
    }
}

?>
